package triggerhelper.controllers;

import javax.swing.JButton;
import javax.swing.JTextArea;
import triggerhelper.SimpleTriggerText;
import triggerhelper.triggerCreator;

public class SimpleTriggerController {

    private JButton bCancel, bOk, bHelp;
    private JTextArea tTriggerText;
    private SimpleTriggerText simpleTrigger;
    private triggerCreator parent;

    public SimpleTriggerController(SimpleTriggerText st, triggerCreator parent_, boolean firstTime){
        simpleTrigger = st;
        parent = parent_;
        setEasyAccessFields();
        setListeners();
        tTriggerText.setText(parent.getCurrentTrigger().getFullTrigger());
        if (firstTime) {
            tTriggerText.setCaretPosition(51);
        } else {
            tTriggerText.setCaretPosition(0);
        }
    }

    private void setEasyAccessFields() {
        bCancel = simpleTrigger.getbCancel();
        bOk = simpleTrigger.getbOk();
        bHelp = simpleTrigger.getbHelp();
        tTriggerText = simpleTrigger.gettTriggerText();
    }

    private void setListeners() {
        bHelp.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                help();
            }
        });
        bCancel.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancel();
            }
        });
        bOk.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                done();
            }
        });
    }

    private void help(){

    }

    private void cancel(){
        simpleTrigger.dispose();
    }

    private void done(){
        parent.getCurrentTrigger().setFullTrigger(tTriggerText.getText());
        parent.getListTriggers().repaint();
        simpleTrigger.dispose();
    }
}
